package com.itboye.shop.me.qianbao;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.adapter.JiaoYiJiLuAdapter;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.JiaoYiJiLuBean;
import com.itboye.shop.common.BasePtr;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.loadutil.IsUtilUid;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 我的账单
 */
public class JiaoYiJiLuActivity extends BaseActivity implements Observer{
    TextView tvTitle;
    ImageView back;

    ListView jiaoyi_list;
    PtrFrameLayout commend_anchor_ptr;
    LinearLayout kong_layoout;
    TextView kong_text;
    JiaoYiJiLuAdapter adapter;

    List<JiaoYiJiLuBean.ListEntity> list;

    YuEPresenter persenter;
    int pageIndex = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiao_yi_ji_lu);
        tvTitle.setText("交易记录");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        list=new ArrayList<JiaoYiJiLuBean.ListEntity>();

        persenter=new YuEPresenter(this);
        showProgressDialog("数据加载中...", true);
        persenter.onZhangDan(IsUtilUid.isUid(),pageIndex+"","10");
        adapter=new JiaoYiJiLuAdapter(this,list);
        jiaoyi_list.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                persenter.onZhangDan(IsUtilUid.isUid(),pageIndex+"","10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                persenter.onZhangDan(IsUtilUid.isUid(),pageIndex+"","10");
            }
        });

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;
        }
    }
    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if (entity != null) {
            if (entity.getEventType()==YuEPresenter.dingdan_success){
                if(pageIndex==1){
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                JiaoYiJiLuBean bean= (JiaoYiJiLuBean) entity.getData();
                list.addAll(bean.getList());

                if (list.size() > 0) {
                    adapter.notifyDataSetChanged();
                    jiaoyi_list.setVisibility(View.VISIBLE);
                    kong_layoout.setVisibility(View.GONE);

                } else {
                    if(pageIndex==1){
                        jiaoyi_list.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                        kong_layoout.setVisibility(View.VISIBLE);
                        kong_text.setText("最近没有消费记录");
                    }
                }
            }
            if (entity.getEventType()==YuEPresenter.dingdan_fail){
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
